import { Component } from '@angular/core';
import {IonicPage, NavController, NavParams, ToastController} from 'ionic-angular';
import {Camera, CameraOptions } from "@ionic-native/camera";
import {ApiProvider} from "../../providers/api/api";
import {FaceProvider} from "../../providers/face/face";
import {LocalStorProvider} from "../../providers/local-stor/local-stor";
import {SpeechRecognition} from "@ionic-native/speech-recognition";

declare let cordova: any;

/**
 * Generated class for the SignUpPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-sign-up',
  templateUrl: 'sign-up.html',
})
export class SignUpPage {

  constructor(public navCtrl: NavController, public navParams: NavParams, public camera: Camera,
              public http: ApiProvider, public face: FaceProvider, public storage: LocalStorProvider,
              public speechRecognition: SpeechRecognition, public toastCtrl: ToastController) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad SignUpPage');
    this.signCamera();
  }

  async signCamera() {

    const user_id = await this.storage.getKey("user_id");

    this.http.signBeforeFace({studentId: user_id, apList: ''}).subscribe(res =>{

      if(res['Code'] == '00000000'){

        this.face.signCamera().then(res=>{

          this.http.searchFace({image: res, user_id: user_id}).subscribe(res => {

            if(res['Code'] == '00000000'){

              this.http.signAfterFaceUnit({studentId: user_id, checkState: true , apList: '', faceRecognition: true}).subscribe(res =>{
                let toast = this.toastCtrl.create({
                  message: res['Message'],
                  duration: 3000,
                  position: 'bottom'
                });

                toast.present();
              }, err=>{

              })
            } else {
              let toast = this.toastCtrl.create({
                message: '识别失败！',
                duration: 3000,
                position: 'bottom'
              });

              toast.present();
            }
          }, err => {

          })

        }, err => {

        })
      } else{
        let toast = this.toastCtrl.create({
          message: '尚未开启签到！',
          duration: 3000,
          position: 'bottom'
        });

        toast.present();
      }

    }, err=>{

    })

  }

  // speech() {
  //   this.speechRecognition.startListening()
  //     .subscribe(
  //       (matches: Array<string>) => alert(matches),
  //       (onerror) => alert('error:', onerror)
  //     )
  //
  //
  // }

}
